package com.hg.admin.action;

import java.io.File;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;



import net.sf.json.JSONObject;

import com.hg.admin.service.AdminService;
import com.hg.admin.vo.VO4admin;
import com.hg.api.ApiService;
import com.hg.cache.FIFOCache;
import com.hg.util.action.MbaseAction;

public class IndexAction extends MbaseAction
{
	
	
	public void index(Map root){
		HttpServletRequest request = (HttpServletRequest) root.get("request");
		HttpSession session = request.getSession();
		String admin_role_id = (String) session.getAttribute("admin_role_id");
		if("0".equals(admin_role_id)){
			root.put("role_name", "开发人员");
			root.put("permission", 0);
		}else{
			root.put("role_name", "系统管理员");
			root.put("permission", 1);
		}
		File file = new File(request.getRealPath("WEB-INF/ftl/admins/inc/role/role_"+admin_role_id+".ftl"));
		if(!file.exists()){
			//文件不存在
			admin_role_id="default";
		}
		root.put("admin_role_id", admin_role_id);
		ftl_return(root);
	}
	
	public void login(Map root){
		if(checkAdminLogin(root)){
			String index_url=getUrl(root, "index");
			js_redirect(root, index_url);
		}else{
			Map<String,String> m_config=getConfigForAdmin(root);
			String supplier_name = m_config.get("supplier_name");
			root.put("supplier_name", supplier_name);
			root.put("isExist", getUrl(root, "isExist"));
			root.put("jump_url", getUrl(root, "index"));
			ftl_return(root);
		}
	}
	
	/**
	 * 退出登录
	 * @param root
	 */
	public void loginout(Map root){
		initLogout(root);
		String login_url =root.get("project_url")+"/"+root.get("folder_name")+"/"+root.get("module")+"/login.do";
		js_redirect(root, login_url);
	}
	
	/**
	 * 清除管理员端,wap端,adminWap端的缓存
	 * @param root
	 */
	public void clearCache(Map root){
		FIFOCache<String, Object> cache = (FIFOCache<String, Object>) root.get("cache");
		cache.remove("m_config");	//清除系统缓存
		
		this.getConfigForAdmin(root);
		map.put("err_code", -1);
		StringBuilder sb  = new StringBuilder();
		sb.append("清除管理员端缓存成功>>>");
		
		ApiService api = new ApiService(root);
		
		//清除wap端的缓存
		String wap_return_json = api.http_get("weixin", "cache/clear.do");
		if(api.isSuccess(wap_return_json)){
			sb.append("清除wap端缓存成功>>>");
		}else{
			sb.append("清除wap端缓存失败>>>");
		}
		
		//清除wap端的缓存
		String api_return_json = api.http_get("api", "cache/clear.do");
		if(api.isSuccess(api_return_json)){
			sb.append("清除api端缓存成功>>>");
		}else{
			sb.append("清除api端缓存失败>>>");
		}
		
		/*
		String weixin_api_return_json = hxc.get(api.weixinApi_url+"cache/clear.do");
		if(api.isSuccess(weixin_api_return_json)){
			sb.append("清除微信api接口端缓存成功>>>");
		}else{
			sb.append("清除微信api接口端缓存失败>>>");
		}
		
		String weixin_withdraw_api_return_json = hxc.get(api.weixin_withdraw_api_url+"cache/clear.do");
		if(api.isSuccess(weixin_withdraw_api_return_json)){
			sb.append("清除微信提现端缓存成功>>>");
		}else{
			sb.append("清除微信提现接口端缓存失败>>>");
		}
		
		String weixin_pay_api_return_json = hxc.get(api.weixin_pay_api_url+"cache/clear.do");
		if(api.isSuccess(weixin_pay_api_return_json)){
			sb.append("清除微信充值端缓存成功>>>");
		}else{
			sb.append("清除微信充值接口端缓存失败>>>");
		}
		
		//还剩微信access_token中控服务器和事件推送服务器
		*/
		
		map.put("err_msg", sb.toString());
		json_return(root, map);
	}
	
	public void isExist(Map root){
		HttpServletRequest request = (HttpServletRequest) root.get("request");
		String username=request.getParameter("username");
		String password=request.getParameter("password");
		
		VO4admin admin=new VO4admin();
		admin.setPassword(password);
		admin.setUsername(username);
		AdminService as=new AdminService();
		JSONObject json=as.loginService(admin);
		
		if(json.getInt("id")==-1){	
			map.put("err_code", 1001);
			map.put("err_msg", "用户不存在");
		}else if(json.getInt("id")==-2){
			map.put("err_code", 1102);
			map.put("err_msg", "密码错误");
		}else{
			root.put("username", username);
			root.put("userId", json.getInt("id"));
			root.put("admin_role_id", json.getInt("admin_role_id"));
			initLogin(root);
			
			map.put("err_code", -1);
			map.put("err_msg", "登录成功");
			map.put("userId", json.getInt("id"));
		}
		
		json_return(root, map);
	}
}
